#define  _CRT_SECURE_NO_WARNINGS 1

#include "Stack.h"

void StackInit(pStack ps)
{
	assert(ps);

	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}


void StackPush(pStack ps, STDataType x)
{
	assert(ps);

	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* temp = (STDataType*)realloc(ps->arr, newcapacity);
		if (temp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		ps->arr = temp;
		ps->capacity = newcapacity;
	}
	ps->arr[ps->size] = x;
	ps->size++;
}


void StackPop(pStack ps)
{
	assert(ps);
	assert(ps->size);
	
	ps->size--;
}


STDataType StackTop(pStack ps)
{
	assert(ps);
	assert(ps->size);

	return ps->arr[ps->size - 1];
}


bool StackEmpty(pStack ps)
{
	assert(ps);

	return ps->size == 0;
}